#include "../../texture/draw.h"
#include "splash.h"

namespace sleek
{
    namespace core
    {
        namespace device
        {
            Splash::Splash(DeviceWindowManager dwm, core::math::vector2du size,bool window_decorator) : windec(window_decorator)
            {
                csp = 0;
                tmcl = 2500;
                Device_stub info = Device_stub(size, 32, false);
                info.decorator = false;
                screen = CreateDeviceWindowManager(dwm,info);
                screen->centerWindowPos();
                screen->enableAntialiasing(device::DAM_NICE);
                screen->setCaption("SleekThink");

                mom = new core::engine::world(screen);
                tm = new math::timer();
            }
            Splash::~Splash()
            {
                if(csp) delete csp;
                delete tm;
                delete mom;
                delete screen;
            }

            void Splash::setPicture(gui::picture *i)
            {
                csp = i;
                mom->getInterface()->addCustomFrame(i);
            }
            gui::picture *Splash::getPicture()
            {
                return csp;
            }
            void Splash::setTimeToClose(int _time)
            {
                tmcl = _time;
            }
            void Splash::resizeFromPicture()
            {
                if(!csp && !csp->getTexture()) return;
                screen->setVideoSize(csp->getTexture()->getOriginalSize());
                screen->centerWindowPos();
            }
            void Splash::resize(core::math::vector2du i)
            {
                screen->setVideoSize(i);
                screen->centerWindowPos();
            }

            void Splash::render()
            {
                while(screen->run() && tm->getTimeMsec() < tmcl/2) // divised by 2 beacause, time is augmented by x Sleeping ms time
                {
                    tm->update_begin();
                    screen->begin();
                        csp->render();
                    screen->end();
                    core::os::Sleeping(2);
                }
            }
        }
    }
}
